﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TFSBranchVisualizer.ServiceInterface;
using TFSBranchVisualizer.DataContracts;
using System.ServiceModel.Activation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TFSBranchVisualizer.ServiceImplementation
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TFSService : ITFSService
    {
        public BranchInfo GetProjectBranches(string projectName)
        {
            BranchInfo currentBranch = GetBranchList(projectName);
            return currentBranch;
        }

        public BranchInfo GetBranchList(string basePath)
        {
            TeamFoundationServer _Server = new TeamFoundationServer("http://hqvstfs1:8080");
            VersionControlServer _VersionControl = (VersionControlServer)_Server.GetService(typeof(VersionControlServer));

            List<string> _Branches = new List<string>();

            BranchHistoryTreeItem[][] _BranchHistory = _VersionControl.GetBranchHistory(new ItemSpec[] { new ItemSpec(basePath, RecursionType.None) }, VersionSpec.Latest);

            BranchHistoryTreeItem _treeItem = null;

            if ((_BranchHistory.Length != 0) && (_BranchHistory[0].Length != 0))
            {
                _treeItem = _BranchHistory[0][0];
            }

            BranchInfo _currentBranch = new BranchInfo();

            Item _currentItem = _VersionControl.GetItem(basePath, VersionSpec.Latest);

            if (_currentItem != null)
            {
                _currentBranch.Name = _currentItem.ServerItem;
            }

            //make sure we have some branches to work with
            if (_treeItem != null)
            {
                _treeItem = _treeItem.GetRequestedItem();
                if (((_treeItem.Parent.Relative.BranchToItem != null)) && (_treeItem.Parent.Relative.BranchToItem.DeletionId == 0))
                {
                    BranchInfo parentBranch = new BranchInfo();
                    parentBranch.Name = _treeItem.Parent.Relative.BranchToItem.ServerItem;
                    //parentBranch.ChildBranches.Add(_currentBranch);
                    _currentBranch.Parent = parentBranch;
                }

                //loop through the all the children (if any)and get the relatives
                foreach (BranchHistoryTreeItem treeItem2 in _treeItem.Children)
                {
                    if ((treeItem2.Relative.BranchToItem != null) && (treeItem2.Relative.BranchToItem.DeletionId == 0))
                    {
                        BranchInfo childBranch = new BranchInfo();
                        childBranch.Name = treeItem2.Relative.BranchToItem.ServerItem;
                        //childBranch.Parent = _currentBranch;
                        _currentBranch.ChildBranches.Add(childBranch);
                    }
                }
            }
            return _currentBranch;

        }
    }
}
